
/*
    Software License

    Copyright (C) 2021-05-24  Xoronos

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, version 3.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

/*
    Liabilities

    The software is provided "AS IS" without any warranty of any kind, either expressed,
    implied, or statutory, including, but not limited to, any warranty that the software
    will conform to specifications, any implied warranties of merchantability, fitness
    for a particular purpose, and freedom from infringement, and any warranty that the
    documentation will conform to the software, or any warranty that the software will
    be error free.

    In no event shall Xoronos be liable for any damages, including, but not limited to,
    direct, indirect, special or consequential damages, arising out of, resulting from,
    or in any way connected with this software, whether or not based upon warranty,
    contract, tort, or otherwise, whether or not injury was sustained by persons or
    property or otherwise, and whether or not loss was sustained from, or arose out of
    the results of, or use of, the software or services provided hereunder.

    To request the provided software under a different license you can contact us at
    support@xoronos.com
*/

#include "confgen.h"

int encoding_compilation_settings( uint64_t encoding_rnd[4]
 )
{

    char buffer1[32];
    char buffer2[32];
    char buffer3[32];
    char buffer4[32];
    char buffer[XRN_MAX_BUFF_SIZE_CONFGEN];
    int i, j;
    int reloop;

    reloop = 0;

    while ( reloop == 0 ) {

        reloop = 1;
        printf( "for the encoding settings type the action (generate/restore): " );
        if ( fgets( buffer, XRN_MAX_BUFF_SIZE_CONFGEN, stdin ) == NULL ) {
            printf( "input output error\n" );
        }

        if ( !strncmp( buffer, "generate", strlen( "generate" ) ) ) {
            printf( "\nour recomandation for new users: random\n" );
            printf( "to generate the encoding settings type the mode (random/default): " );
            if ( fgets( buffer, XRN_MAX_BUFF_SIZE_CONFGEN, stdin ) == NULL ) {
                printf( "input output error\n" );
            }

            if ( !strncmp( buffer, "random", strlen( "random" ) ) ) {
                if ( get_random_bytes( ( uint8_t * ) & encoding_rnd[0], sizeof( uint64_t ) * 4 ) != XSUCCESS ) {
                    printf( "io error\n" );
                    return XERR_IO;
                }

                printf
                    ( "to restore the encoding configuration in a future compilation, copy the following key.\n" );

                base64_encode( ( char * ) &encoding_rnd[0], sizeof( uint64_t ) * 4, buffer );
                prettify_b64( buffer );

                printf( "\nencoding key: " );
                for ( j = 0; j < strlen( buffer ) / 4; j++ ) {
                    printf( "%c", buffer[j] );
                }

                printf( " " );
                for ( j = ( uint8_t ) ( strlen( buffer ) / 4 ); j < strlen( buffer ) / 2; j++ ) {
                    printf( "%c", buffer[j] );
                }

                printf( " " );
                for ( j = ( uint8_t ) ( strlen( buffer ) / 2 ); j < 3 * strlen( buffer ) / 4; j++ ) {
                    printf( "%c", buffer[j] );
                }

                printf( " " );
                for ( j = ( uint8_t ) ( 3 * strlen( buffer ) / 4 ); j < strlen( buffer ); j++ ) {
                    printf( "%c", buffer[j] );
                }

                printf( "\n\n" );

            } else if ( !strncmp( buffer, "default", strlen( "default" ) ) ) {
                printf( "default configuration selected.\n" );
            } else {
                printf( "option not understood.\n" );
                reloop = 0;
            }

        } else if ( !strncmp( buffer, "restore", strlen( "restore" ) ) ) {

            printf( "encoding key : " );
            if ( fgets( buffer, XRN_MAX_BUFF_SIZE_CONFGEN, stdin ) == NULL ) {
                printf( "input output error\n" );
            }

            sscanf( buffer, "%s %s %s %s", buffer1, buffer2, buffer3, buffer4 );
            printf( "\n" );

            if ( ( strlen( buffer1 ) != 11 ) || ( strlen( buffer2 ) != 11 ) || ( strlen( buffer3 ) != 11 )
                 || ( strlen( buffer4 ) != 11 ) ) {
                printf( "option not understood.\n" );
                reloop = 0;
            }

            for ( i = 0; i < 11; i++ ) {
                buffer[0 * 11 + i] = buffer1[i];
                buffer[1 * 11 + i] = buffer2[i];
                buffer[2 * 11 + i] = buffer3[i];
                buffer[3 * 11 + i] = buffer4[i];
            }

            buffer[44] = '\0';
            deprettify_b64( buffer );
            if ( base64_decode( buffer, ( int ) strlen( buffer ), ( char * ) &encoding_rnd[0] ) != XSUCCESS ) {

                printf( "wrong formatted key.\n" );
                reloop = 0;

            } else {

                printf( "encoding key restored.\n" );

            }

        } else {

            printf( "option not understood.\n" );
            reloop = 0;

        }

    }

    return XSUCCESS;
}

int print_encoding_compilation_settings( uint64_t encoding_rnd[4], FILE * encoding_settings_fp
 )
{

    xrn_encoding_param_t encoding_param;
    xrn_encoding_tmp_param_t enc_tmp;
    int i, j;
    char buffer[256];
    uint32_t high;
    uint32_t low;

    ////////////////////////////
    // Generate encoding file //
    ////////////////////////////

    for ( i = 0; i < sizeof( uint64_t ) * 4; i++ ) {
        buffer[i] = ( char ) ( ( ( uint8_t * ) & encoding_rnd[0] )[i] );
    }

    initilize_enc_param_confgen( &encoding_param );
    encoding_param.prob_checks.check_running_error_en = 0;
    encoding_param.prob_checks.check_disparity_error_en = 0;

    // shuffle key times
    for ( j = 0; j < sizeof( uint64_t ) * 4; j++ ) {
        xrn_encode( &encoding_param, ( uint8_t * ) buffer, ( uint8_t * ) buffer, sizeof( uint64_t ) * 4,
                    &enc_tmp );
    }

    for ( i = 255; i >= 0; i-- ) {
        buffer[i] = ( char ) i;
    }

    // shuffle 256 times
    for ( j = 0; j < 256; j++ ) {
        xrn_encode( &encoding_param, ( uint8_t * ) buffer, ( uint8_t * ) buffer, 256, &enc_tmp );
    }

    // rewrite counter
    for ( j = 0; j < 32; j++ ) {
        ( ( uint8_t * ) & encoding_param.prng_state[0] )[j] = ( uint8_t ) buffer[j];
    }

    // shuffle 256 times
    for ( j = 0; j < 256; j++ ) {
        xrn_encode( &encoding_param, ( uint8_t * ) buffer, ( uint8_t * ) buffer, 256, &enc_tmp );
    }

    if ( fprintf
         ( encoding_settings_fp,
           "#ifndef XRN_ENCODING_SETTINGS_H\n#define XRN_ENCODING_SETTINGS_H\n#include <stdint.h>\nstatic const uint8_t STD_ENC_PARAM_TABLE[] = {\n" )
         < 0 ) {
        printf( "io error\n" );
        return XERR_IO;
    }

    for ( i = 0; i < 256; i++ ) {
        if ( i != 255 ) {
            if ( fprintf( encoding_settings_fp, " UINT8_C( %3u ), ", encoding_param.table[i] ) < 0 ) {
                printf( "io error\n" );
                return XERR_IO;
            }

        } else {
            if ( fprintf
                 ( encoding_settings_fp,
                   " UINT8_C( %3u ) };\nstatic const uint32_t STD_ENC_PARAM_CNT[] = {\n",
                   encoding_param.table[i] ) < 0 ) {
                printf( "io error\n" );
                return XERR_IO;
            }

        }

    }

    for ( i = 0; i < 4; i++ ) { // Iterate over each uint64_t
        // Split uint64_t into two uint32_t values (high and low 32 bits)
        high = ( uint32_t ) ( encoding_param.prng_state[i] >> 32 );     // Upper 32 bits
        low = ( uint32_t ) ( encoding_param.prng_state[i] & 0xFFFFFFFF );       // Lower 32 bits

        // Print high part
        if ( fprintf( encoding_settings_fp, "UINT32_C(%u), ", high ) < 0 ) {
            printf( "io error\n" );
            return XERR_IO;
        }

        // Print low part, with special handling for the last element
        if ( i == 3 ) {         // Last uint64_t, last uint32_t
            if ( fprintf( encoding_settings_fp, "UINT32_C(%u) };\n", low ) < 0 ) {
                printf( "io error\n" );
                return XERR_IO;
            }

        } else {
            if ( fprintf( encoding_settings_fp, "UINT32_C(%u), ", low ) < 0 ) {
                printf( "io error\n" );
                return XERR_IO;
            }

        }

    }

    if ( fprintf( encoding_settings_fp, "\n#endif\n" ) < 0 ) {
        printf( "io error\n" );
        return XERR_IO;
    }

    return XSUCCESS;
}

